package com.ita.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ita.activemq.Sender;
import com.ita.annotation.Login;
import com.ita.annotation.Register;
import com.ita.dao.MUserDao;
import com.ita.pojo.MUser;
import com.ita.service.MUserService;
import com.ita.util.JacksonUtil;

@Service(value = "muserService")
public class MUserServiceImpl implements MUserService {

	@Resource(name="merchantDao")
	private MUserDao mDao;

	@Resource(name="sender")
	private Sender sender;

	@Override
	@Transactional
	@Register(description="register")
	public int regist(MUser mUser) {

		MUser m = mDao.add(mUser);
		return 1;
	}

	@Override
	@Transactional
	public MUser update(MUser mUser) {

		MUser newUser = mDao.update(mUser);
		return newUser;
	}

	@Override
	@Transactional
	@Login(description="login")
	public MUser login(String name, String pwd) {

		MUser m = mDao.getByName(name);
		if (m == null) {
			return null;
		}
		if (m.getPwd().equals(pwd))
			return m;
		else
			return null;
	}

	@Override
	@Transactional
	public int updateBlock(String id, int block) {

		int m = mDao.UpdateBlock(id, block);
		return m;
	}

	@Override
	public int sendToMQ(MUser mUser) {

		mUser.setSubmit_time(new Date().getTime());
		String json = JacksonUtil.getJsonFromObject(mUser);
		return sender.sendMessage(json);
	}

	@Override
	@Transactional
	public MUser getByName(String name) {

		return mDao.getByName(name);
	}

	@Override
	public MUser getById(String id) {
		return mDao.getById(id);
	}

	@Transactional
	@Override
	public MUser updateInfo(String id, int block, int state) {
		// TODO Auto-generated method stub
		MUser m = mDao.update(id,block,state);
		return m;
	}

}
